#!/usr/local/bin/python
# -*-encoding:utf-8 -*-

predict_dict = {}

predict = open('BM25_Results/results', 'r');
for line in predict:
    li = line.strip().split('\t');
    queryID = li[0];
    appID = li[2]
    queryString = li[1];
    res = float(li[3]);
    if predict_dict.has_key(queryString):
        predict_dict[queryString][appID] = res;
    else:
        predict_dict[queryString] = {};
        predict_dict[queryString][appID] = res;
predict.close();


labeled = open('LabeledData/label_from_log.txt', 'r');

rmls_results = open('Results/rmls_results', 'w');
c = 0;
for line in labeled:
    li = line.strip().split('|');
    c += 1;
    queryID = str(c);
    l = len(li);
    queryString = li[0];
    for i in range(1,l):
        cli = li[i].split(':');
        
        if predict_dict.has_key(queryString) and predict_dict[queryString].has_key(cli[1]):
            rmls_results.write('%s\t%s\t%s\t%s\t%f\n' % (queryID, queryString, cli[1], int(cli[2]), predict_dict[queryString][cli[1]]));
        else:
            rmls_results.write('%s\t%s\t%s\t%s\t%f\n' % (queryID, queryString, cli[1], int(cli[2]), 0));
            

labeled.close();
rmls_results.close();
